<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CouponsConfig;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\CouponsConfig  AS CouponsConfigModel;
use App\Admin\RowActions\CouponsSendAction;
class CouponsConfigController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CouponsConfig(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('descript');
            $grid->column('expire_day')->editable();
            $grid->column('type')->display(function ($type){
                return CouponsConfigModel::$couponsTypeMap[$type];
            });
            $grid->column('required_days');
//            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like("title");
                $filter->equal("type","券的性质")->select(
                  CouponsConfigModel::$couponsTypeMap
                );
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $coupons_config_id = $actions->row->id;
//                dd($coupons_config_id);
                // append一个操作
                $url=<<<HTML
        <a href="/cpgdeal/coupons/create_by_user/${coupons_config_id}"><i class="fa fa-mail-forward"></i>给指定人发送礼品券</a>
HTML;

                $actions->append($url);
                $actions->append(new CouponsSendAction());
                // prepend一个操作
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CouponsConfig(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('descript');
            $show->field('expire_day');
            $show->type()->using(
                CouponsConfigModel::$couponsTypeMap
            );
            $show->field('required_days');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CouponsConfig(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->text('descript');
            $form->number('expire_day');
            $form->select('type')->options(
                CouponsConfigModel::$couponsTypeMap
            )->rules(function ($form){
                //注册赠礼的话仅能有一个礼券
                if ($form->type == CouponsConfigModel::COUPONS_TYPE_REGISTER){
//                    return "unique:coupons_config,type,".$form->model()->id;
                    $id = CouponsConfigModel::where('type',$form->type)->value("id");
                    if ($id&&$form->model()->id != $id){
                        $form->responseValidationMessages('type', '注册类型的礼品券仅能有一个');
                    }
                }
            });
            $form->number('required_days')->rules(function ($form){
                if ($form->type == CouponsConfigModel::COUPONS_TYPE_SIGNIN) { //签到即送
                    if ($form->required_days<=0){
                        $form->responseValidationMessages('required_days', '当性质为签到送礼时签到天数必须');
                    }
                }
                });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
